package de.homelabs.hlmodules.cms.template.service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.log4j.Logger;

import de.homelabs.hlmodules.cms.api.template.service.IHLTemplateImportStrategy;
import de.homelabs.hlmodules.cms.configuration.domain.HLConfigurationEntity;
import de.homelabs.hlmodules.cms.enums.HLErrorCode;
import de.homelabs.hlmodules.cms.template.domain.HLTemplate;
import de.homelabs.hlmodules.cms.template.domain.HLTemplateException;

public class HLTemplateFileSystemLoadingStrategy implements
		IHLTemplateImportStrategy {

	final static Logger logger = Logger
			.getLogger(HLTemplateFileSystemLoadingStrategy.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homelabs.hlmodules.template.service.IHLTemplateImportStrategy#
	 * findTemplateSource(java.lang.String)
	 */
	public HLTemplate findTemplateSource(HLConfigurationEntity configEntity,
			String normalizedDomain, String name) throws HLTemplateException {

		// convert to filename /templatename
		String fileName = configEntity.getConfigPath() + "//"
				+ normalizedDomain + "//templates//" + name;
		File file = new File(fileName);
		if (file.exists()) {
			HLTemplate template = new HLTemplate();

			// set object data
			template.setName(file.getPath());
			template.setKey(fileName);
			template.setModificationDate(file.lastModified());

			// Return
			return template;
		} else {
			throw new HLTemplateException(HLErrorCode.TEMPLATE_NOT_FOUND);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homelabs.hlmodules.template.service.IHLTemplateImportStrategy#
	 * getLastModified(de.homelabs.hlmodules.template.domain.HLTemplate)
	 */
	public long getLastModified(HLTemplate templateSource) throws HLTemplateException {
		// since we have files here, they can be changed
		// return templateSource.getModificationDate();
		try {
			File template = new File(templateSource.getName());
			return template.lastModified();
		} catch (Exception e) {
			// possible file errors
			logger.error("-getLastModified-: " + e);
			throw new HLTemplateException(HLErrorCode.TEMPLATE_DETAIL_ERROR);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.homelabs.hlmodules.template.service.IHLTemplateImportStrategy#getReader
	 * (de.homelabs.hlmodules.template.domain.HLTemplate, java.lang.String)
	 */
	public Reader getReader(HLTemplate templateSource, String encoding)
			throws HLTemplateException {
		File file = new File(templateSource.getKey());
		if (file.exists()) {
			// prepare buffer
			byte[] buffer = new byte[(int) file.length()];
			InputStream in;
			
			//read content
			try {
				in = new FileInputStream(file);
				in.read(buffer);
				in.close();
			} catch (Exception e) {
				throw new HLTemplateException(HLErrorCode.TEMPLATE_DETAIL_ERROR);
			}

			// set content on Template
			templateSource.setContent(buffer);

			// return reader to content
			return new InputStreamReader(new ByteArrayInputStream(
					templateSource.getContent()));
		} else {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homelabs.hlmodules.template.service.IHLTemplateImportStrategy#
	 * closeTemplateSource(de.homelabs.hlmodules.template.domain.HLTemplate)
	 */
	public void closeTemplateSource(HLTemplate templateSource)
			throws HLTemplateException {
		// nothing to do yet :-)
	}
}
